package com.wf.it.hpweb;

import com.wf.it.common.api.DeptService;
import com.wf.it.common.api.DoctorService;
import com.wf.it.common.entity.Doctor;
import com.wf.it.common.util.ForeignHelper;
import com.wf.it.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "DoctorController-医生信息管理")
public class DoctorController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DoctorService docService;
    @Autowired
    private DeptService deptService;

    @ApiOperation("根据医生编号查看一个医生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="empno",value="医生的员工编号",paramType = "query")
    })
    @GetMapping("doc/findbyempno")
    public  Result showByEmpNo(String empno){
        Result result=docService.findByEmpNo(empno);

        if(result.getState()==600)
            return result;
        ForeignHelper.fillMap(result,deptService
                ,"findByName","deptName","map","dept");
        return result;
    }

    @ApiOperation("获取所有医生信息")
    @GetMapping("/doc/all")
    public Result<List<Doctor>> showAll(){

        Result<List<Doctor>> docsResult=  docService.findAll();
        if(docsResult.getState()!=200)
            return docsResult;
        ForeignHelper.fillMap(docsResult,deptService,
                "findByNames","deptName","map","dept");
//        //拿到外键的结果
//        Result deptsResult=deptService.findByNames(ForeignHelper.getStringKeys(docsResult,"deptName"));
//        //对号入座
//        ForeignHelper.fillMap(docsResult,deptsResult,
//                "deptName","map","dept");
        return docsResult;
    }

    /*@GetMapping("/doc/all")
    public Result<List<Doctor>> showAll(){

        Result<List<Doctor>> docsResult=  docService.findAll();
        if(docsResult.getState()!=200)
            return docsResult;

        List<Doctor> list= (List<Doctor>) docsResult.getObj();
        Set<String> set=new HashSet();
        for(Doctor doc:list)
            set.add(doc.getDeptName());
        String[] names=new String[set.size()];
        set.toArray(names);

        Result deptsResult=deptService.findByNames(names);
        Map<String,Dept> map= (Map<String, Dept>) deptsResult.getObj();
        for(Doctor doc:list) {
            if(deptsResult.getState()==200)
                doc.getMap().put("dept", map.get(doc.getDeptName()));
            else
                doc.getMap().put("dept_err",deptsResult.getMsg());
        }
        return docsResult;
    }*/

    /*@GetMapping("/doc/all")
    public Result showAll(){
        Result docsResult=  docService.findAll();
        if(docsResult.getState()!=200)
            return docsResult;

        List<Map<String,Object>> list= (List<Map<String,Object>>) docsResult.getObj();
        Set<String> set=new HashSet();
        for(Map<String,Object> doc:list)
            set.add((String)doc.get("deptName"));
        String[] names=new String[set.size()];
        set.toArray(names);

        Result deptsResult=deptService.findByNames(names);
        Map<String,Dept> map= (Map<String, Dept>) deptsResult.getObj();
        for(Map<String,Object> doc:list) {
           if(deptsResult.getState()==200)
               ((Map<String,Object>)doc.get("map")).put("dept", map.get(doc.get("deptName")));
           else
               ((Map<String,Object>)doc.get("map")).put("dept_err",deptsResult.getMsg());
        }
        return docsResult;


    }*/
    @ApiOperation("根据部门名称获取医生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="deptname",value="部门名称",paramType = "query")
    })
    @GetMapping("doc/findbydept")
    public Result showByDept(String deptname){
        Result docsResult= (Result) redisTemplate.opsForValue().get("doclist_"+deptname);
        if(docsResult!=null)
            return docsResult;

        docsResult= docService.findByDept(deptname);
        if(docsResult.getState()!=200)
            return docsResult;
        ForeignHelper.fillMap(docsResult,deptService,
                "findByNames","deptName","map","dept");
        redisTemplate.opsForValue().set("doclist_"+deptname,docsResult,6, TimeUnit.HOURS);
//        Result deptResult=deptService.findByName(deptname);
//        List<Doctor> list= (List<Doctor>) docsResult.getObj();
//
//        for(Doctor doc:list){
//            doc.getMap().put("dept",deptResult.getObj());
//            if(deptResult.getState()!=200)
//                doc.getMap().put("dept_err",deptResult.getMsg());
//        }
        return docsResult;
    }

    @ApiOperation("根据医生id获取一个医生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="docid",value="医生id",paramType = "query")
    })
    @GetMapping("doc/findbyid")
    public  Result showDetail(int docid){
        Result result=docService.findById(docid);
        if(result.getState()==600)
            return result;
        ForeignHelper.fillMap(result,deptService
                ,"findByName","deptName","map","dept");
       /* if(result.getState()!=600){
           Doctor doc= (Doctor) result.getObj();
           Result deptResult=deptService.findByName(doc.getDeptName());
           if(deptResult.getState()==200)
              doc.getMap().put("dept",deptResult.getObj());
           else
              doc.getMap().put("dept_err",deptResult.getMsg());
        }*/
        return result;
    }


}
